/**
 * 驼峰转中横线
 */
export function camelCase2KebabCase(name: string) {
  return name.replace(/([A-Z])/g, '-$1').toLowerCase();
}

/**
 * 字符串脱敏(替换为星号)
 * @param str - 需要脱敏的字符串
 * @param beginLen - 前面可见的位数
 * @param endLen - 后面可见的位数
 * @param lenShow - 当总长度不足是是否显示*
 * @param maxAsterisk - 最多保留几个星号
 */
export function strDsensitization(str: string, beginLen: number, endLen: number, lenShow?: boolean, maxAsterisk?: number) {
  const len = str.length;
  if (len <= beginLen + endLen && !lenShow) {
    return str;
  } else {
    let firstStr = str.slice(0, beginLen);
    let middleStr = '';
    let lastStr = '';

    if (len > beginLen + endLen) {
      lastStr = str.slice(-endLen);
      middleStr = str.substring(beginLen, len - Math.abs(endLen)).replace(/[\s\S]/gi, '*');
    } else if (len > beginLen) {
      middleStr = str.substring(beginLen, len).replace(/[\s\S]/gi, '*');
    } else {
      firstStr = str.replace(/[\s\S]/gi, '*');
    }
    if (typeof maxAsterisk === 'number' && maxAsterisk > 0 && middleStr.length > 0) {
      middleStr = middleStr.slice(0, maxAsterisk);
    }
    return `${firstStr}${middleStr}${lastStr}`;
  }
}
